//
//  UserTool.swift
//  JiaXiaoTong
//
//  Created by 沈增光 on 2018/2/2.
//  Copyright © 2018年 IT. All rights reserved.
//

import UIKit


class UserTool: NSObject {
  
    
   
}
extension  UserTool {
    
    /**
     *  存储用户信息
     */
     
    class func saveUserInfo(userinfo:UserInfoModel){
        
        NSKeyedArchiver.archiveRootObject(userinfo, toFile:YJUserInfoPath )
    }

    
//    /**
//     *  存储家长。 用户信息
//     */
    
    class func saveParents(parents:Parents){
        
        //NSKeyedArchiver.archiveRootObject(Parents(), toFile: YJParentsInfoPath)
    }
    
//    + (void)saveParents:(Parents *)parents
//    {
//    [NSKeyedArchiver archiveRootObject:parents toFile:YJParentsInfoPath];
//    }
//    
//    /**
//     *  返回老师信息
//     */
    class func  takeOutTeacher ()->JXTeacher? {
  
        let userInfo  = NSKeyedUnarchiver.unarchiveObject(withFile: YJUserInfoPath)
        guard (userInfo == nil ) else {
            
            
            return (userInfo as! UserInfoModel).teacher
        }
        return nil 
    }
//    +(JXTeacher *)takeOutTeacher
//    {
//    UserInfoModel *userInfo=[NSKeyedUnarchiver unarchiveObjectWithFile:YJUserInfoPath];
//    return userInfo.teacher;
//    }
//    
//    /**
//     *  返回学生班级信息
//     */
    
    class func takeOutClassCourseList()->NSArray{
        let userInfo = NSKeyedUnarchiver.unarchiveObject(withFile: YJUserInfoPath) as! UserInfoModel
        return userInfo.apiClassList!
    }
    
//    +(NSArray *)takeOutClassCourseList
//    {
//    
//    UserInfoModel *userInfo=[NSKeyedUnarchiver unarchiveObjectWithFile:YJUserInfoPath];
//    return userInfo.apiClassList;
//    }
//    /**
//     *  返回学生课程级别
//     */
    
    class  func levelName()->NSString{
        let userInfo = NSKeyedUnarchiver.unarchiveObject(withFile: YJUserInfoPath) as! UserInfoModel
        let course = (userInfo.apiClassList?.firstObject) as!  ClassCourse
        return course.levelName as! NSString ?? "" as NSString
        
    }
//    +(NSString *)levelName
//    {
//    UserInfoModel *userInfo=[NSKeyedUnarchiver unarchiveObjectWithFile:YJUserInfoPath];
//    ClassCourse *course = [userInfo.apiClassList firstObject];
//    return course.levelName?course.levelName : @"";
//    }
//    
//    /**
//     *  返回UserInfoModel
//     */
    
    class  func UserInfoModel()-> UserInfoModel {
        let userInfo : UserInfoModel = NSKeyedUnarchiver.unarchiveObject(withFile: YJUserInfoPath) as! UserInfoModel 
        return userInfo
    }
//    +(UserInfoModel *)UserInfoModel
//    {
//    UserInfoModel *userInfo=[NSKeyedUnarchiver unarchiveObjectWithFile:YJUserInfoPath];
//    return userInfo;
//    }
//    
//    /**
//     *  返回家长。 用户信息 Parents
//     */
    class func parents ()->Parents? {
        let  par  = NSKeyedUnarchiver.unarchiveObject(withFile: YJParentsInfoPath)
        guard   ( par == nil) else{
            
            return (par as! Parents)
        }
        return nil
        
    }
//    + (Parents *)parents
//    {
//    Parents *par = [NSKeyedUnarchiver unarchiveObjectWithFile:YJParentsInfoPath];
//    return par;
//    }
//    /**
//     *  返回家长。 班级列表 levelNames
//     */
    class func  classCourseListlevelNames()->NSArray {
        
        let  lists  = NSMutableArray()
        for course in  UserTool.classCourseList(){
            
            lists.add(course as! ClassCourse)
            
        }
        return lists
    }
//    +(NSArray *)classCourseListlevelNames
//    {
//    NSMutableArray *lists = [NSMutableArray array];
//    for (ClassCourse *class in [UserTool classCourseList]) {
//    [lists addObject:class.levelName];
//    }
//    return lists;
//    }
//    
//    /**
//     *  返回家长。 班级列表 ClassCourse
//     */
    class func classCourseList()->NSArray {
        let parents  = UserTool.parents()
        return parents!.classCourseList! ;
    }
//    +(NSArray *)classCourseList
//    {
//    Parents *parents =[UserTool parents];
//    return parents.classCourseList;
//    }
//    /**
//     *  返回家长。  返回  班级信息 ClassCourse
//     */
    class func classCourse()->ClassCourse{

        let index = NSObject.tp_object(forKey: "ClassCourseIndex")
        let classCourse = UserTool.classCourseList()[index as! Int]
        return classCourse as! ClassCourse
        
    }
//    + (ClassCourse *)classCourse
//    {
//    NSInteger index = [NSObject tp_integerForKey:@"ClassCourseIndex"];
//    ClassCourse *class = [UserTool classCourseList][index];
//    return  class;
//    }
//    /**
//     *  清空用户信息
//     */
    class func removeUserInfo(){
        
       UserTool.notiWithState(state: "logout")
       UserTool .setApplicationIconBadgeNumber(badgeNumber: 0)
        let file  = FileManager.default
        do {
            
            try file.removeItem(atPath: YJParentsInfoPath)
            
            
        }catch {
            
            
        }
        do {
            try  file.removeItem(atPath: YJUserInfoPath)
        }catch{}
        do {
            
            try file.removeItem(atPath: YJSaveAnnouncementoPath)
        }catch{}
       // file.removeItem(atPath: <#T##String#>)
        
    }
//    + (void)removeUserInfo
//    {
//    [UserTool notiWithState:@"logout"];
//    [UserTool setApplicationIconBadgeNumber:0];
//    NSFileManager *file=[NSFileManager defaultManager];
//    [file removeItemAtPath:YJParentsInfoPath error:nil];
//    [file removeItemAtPath:YJUserInfoPath error:nil];
//    [file removeItemAtPath:YJSaveAnnouncementoPath error:nil];
//    }
//    
//    
//    /**
//     发送通知 通知极光接受通知  state:login  logout
//     */
    
    class func notiWithState (state: NSString) {
        
        let index  =  NSObject.tp_object(forKey: "teacherIndex") as! Int
        let noti = NSMutableDictionary()
        if index==0{
        
        let userinfo = self.UserInfoModel()
           noti["userId"] = userinfo.teacher?.teacherID
            
        }else {
            
            let  parent = self.parents()
            noti["userId"] = parent?.apiStudent?.id
        }
        noti["state"] = state
        
        NotificationCenter.default.post(name: NSNotification.Name(rawValue: kNotificationLoginOrLogOut) , object: noti)
    }
    
//    +(void)notiWithState:(NSString *)state
//    {
//    NSInteger index = [NSObject tp_integerForKey:@"teacherIndex"];
//    
//    NSMutableDictionary *noti = [NSMutableDictionary dictionary];
//    if (index==0) {//老师登录
//    UserInfoModel *userinfo = [self UserInfoModel];
//    noti[@"userId"] = userinfo.teacher.teacherID;
//    }else{//家长登录
//    Parents *parent = [self parents];
//    noti[@"userId"] = parent.apiStudent.id;
//    }
//    noti[@"state"] = state;
//    [[NSNotificationCenter defaultCenter] postNotificationName:kNotificationLoginOrLogOut object:noti];
//    }
//    
//    /**
//     设置角标
//     */
    
    class func setApplicationIconBadgeNumber( badgeNumber : NSInteger){
        
        JPUSHService.setBadge(badgeNumber)
        UIApplication.shared.applicationIconBadgeNumber = badgeNumber
        
        
    }
//    +(void)setApplicationIconBadgeNumber:(NSInteger)badgeNumber
//    {
//    [JPUSHService setBadge:badgeNumber];
//    [UIApplication sharedApplication].applicationIconBadgeNumber = badgeNumber;
//    }
//    #pragma mark 草稿箱
//    /**
//     *  存储草稿箱。公告信息
//     */
    class func saveDraftAnnouncement (model : SaveAnnouncementModel ){
        
        var  array = NSKeyedUnarchiver.unarchiveObject(withFile: YJSaveAnnouncementoPath) as! NSMutableArray
        if (array != nil) {
            array = NSMutableArray()
            
        }
        
        if  array.count >= 20 {
            
            return
        }
    
        model.createTime = NSString.getCurrentTime()
        array.insert(model, at: 0)
        NSKeyedArchiver .archiveRootObject(array, toFile: YJSaveAnnouncementoPath)
        
    }
//    + (void)saveDraftAnnouncement:(SaveAnnouncementModel *)model
//    {
//    NSMutableArray *array = [NSKeyedUnarchiver unarchiveObjectWithFile:YJSaveAnnouncementoPath];
//    if (!array) {
//    array = [NSMutableArray array];
//    }
//    if (array.count >= 20) {
//    return;
//    }
//    model.createTime = [NSString getCurrentTime];
//    [array insertObject:model atIndex:0];
//    [NSKeyedArchiver archiveRootObject:array toFile:YJSaveAnnouncementoPath];
//    }
//    
//    /**
//     返回所有的草稿箱信息
//     */
    class func backAnnouncementDrafts()->NSMutableArray {
        
        let array  = NSKeyedUnarchiver .unarchiveObject(withFile: YJSaveAnnouncementoPath)
        return array as! NSMutableArray
    }
//    + (NSMutableArray *)backAnnouncementDrafts
//    {
//    NSMutableArray *array = [NSKeyedUnarchiver unarchiveObjectWithFile:YJSaveAnnouncementoPath];
//    return array;
//    }
//    /**
//     *  删除草稿箱其中一条信息
//     */
    class func removeAnnouncementDraft (model : SaveAnnouncementModel){
        
        let array = NSKeyedUnarchiver.unarchiveObject(withFile: YJSaveAnnouncementoPath) as! NSMutableArray
        
        array.remove(model);
        NSKeyedArchiver.archiveRootObject(array, toFile: YJSaveAnnouncementoPath)
    }
//    +(void)removeAnnouncementDraft:(SaveAnnouncementModel *)model
//    {
//    NSMutableArray *array = [NSKeyedUnarchiver unarchiveObjectWithFile:YJSaveAnnouncementoPath];
//    [array removeObject:model];
//    [NSKeyedArchiver archiveRootObject:array toFile:YJSaveAnnouncementoPath];
//    }
//    /**
//     *  删除草稿箱 第几条信息
//     */
    class func removeAnnouncementDraftAtIndex (index : Int) {
        
        let array  = NSKeyedUnarchiver.unarchiveObject(withFile: YJSaveAnnouncementoPath) as! NSMutableArray
        
         array.removeObject(at: index)
        NSKeyedArchiver.archiveRootObject(array, toFile: YJSaveAnnouncementoPath)
    }
//    +(void)removeAnnouncementDraftAtIndex:(NSUInteger )index
//    {
//    NSMutableArray *array = [NSKeyedUnarchiver unarchiveObjectWithFile:YJSaveAnnouncementoPath];
//    [array removeObjectAtIndex:index];
//    [NSKeyedArchiver archiveRootObject:array toFile:YJSaveAnnouncementoPath];
//    }
//    /**
//     清空草稿箱所有信息
//     */
    class func removeAnnouncementDraft (){
        let file  =  FileManager.default
        //file.removeItem(atPath: <#T##String#>)
        do {
            
            try file.removeItem(atPath: YJSaveAnnouncementoPath)
        }catch{
            
            print("error")
            
        }
        
            
            
            
    
        
    }
//    +(void)removeAnnouncementDraft
//    {
//    NSFileManager *file=[NSFileManager defaultManager];
//    [file removeItemAtPath:YJSaveAnnouncementoPath error:nil];
//    }
//    
    
    
    
    
    
    
    
}
